import random
from django.core.management.base import BaseCommand
from hostmanager.models import Host

class Command(BaseCommand):
    help = 'Statistics for daily host count'

    def handle(self, *args, **kwargs):
        # 按城市和机房维度统计主机数量
        from collections import defaultdict
        
        data_center_count = defaultdict(int)
        city_count = defaultdict(int)
        
        hosts = Host.objects.select_related('data_center', 'data_center__city').all()
        for host in hosts:
            data_center_count[host.data_center.name] += 1
            city_count[host.data_center.city.name] += 1
        
        self.stdout.write(f'DataCenter Count: {dict(data_center_count)}')
        self.stdout.write(f'City Count: {dict(city_count)}')